package im.qingji.example;

import im.qingji.sqlutil.SQLExecutor;
import im.qingji.sqlutil.connection.ConnectionFactory;
import im.qingji.sqlutil.support.ConfigPrefix;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 
 * @author hetao
 * @version 0.1 <br>
 *          Created on 2011-7-20 下午06:51:23
 */
public class HelloWorld {

	public static void main(String[] args) throws SQLException {
		Connection conn = null;
		try {
			conn = ConnectionFactory.getConnection(ConfigPrefix.ORACLE);
			// 获取默认连接
			if (conn == null) {
				System.err.println("无法获取数据库连接，请确认数据库服务正常，或配置正确。");
			}
			SQLExecutor executor = new SQLExecutor(conn);
			// 创建数据库映射表
			String createTable = " create table test_user " 
					+ "( id int, name varchar2(20), "
					+ " password varchar2(20))";
			try {
				executor.execute(createTable);
			} catch (Exception ignore) {
			}
			// ps. User对象需与数据库表test_user映射。
			// 创建并插入User
			User user = new User();
			user.setId(1);
			user.setName("Tom");
			user.setPassword("pwd");
			// 插入
			int count = executor.insert(user);
			System.out.println("成功执行了" + count + " 条语句");
			user = null;
			// 查询
			user = executor.queryObject("select * from test_user where id = 1", User.class);
			System.out.println(user.getName());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.close();
			}
		}
	}
}
